package main

import (
	"fmt"
	"math"
)

//这是一个几何体的基本接口,定义了2个方法 面积和周长
type geometry interface {
	area() float64
	perim() float64
}

//我们将在rect和circle上实现这个接口，结构体
type rect struct {
	width, height float64
}
type circle struct {
	radius float64
}

//长方形的2个接口 面积和周长
func (r rect) area() float64 {
	return r.width * r.height
}

func (r rect) perim() float64 {
	return 2 * (r.width + r.height)
}

// 圆形的2个接口 面积和周长
func (c circle) area() float64 {
	return math.Pi * c.radius * c.radius
}
func (c circle) perim() float64 {
	return 2 * math.Pi * c.radius
}

func measure(g geometry) {
	fmt.Println(g)
	fmt.Println(g.area())
	fmt.Println(g.perim())
}

func main() {
	r := rect{3, 4}
	c := circle{5}
	measure(r)
	measure(c)

	printArea(r)
	printArea(c)

}

//接口断言的用法
func printArea(g geometry) {
	//直接判断它为矩形
	if rect, ok := g.(rect); ok {
		fmt.Println("rect area is = ", rect.area())
	} else if circle, ok := g.(circle); ok {
		fmt.Println("circle area is = ", circle.area())
	} else {
		fmt.Println("unknow shape")
	}
}
